package com.yto.hrreport.command {
	import com.adobe.serialization.json.JSON;
	import com.yto.hrreport.pub.IProcessor;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.TitleWindow;
	import mx.controls.Button;
	import mx.controls.DataGrid;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.managers.PopUpManager;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class ReportCommand {
		
		private static var reportCommand : ReportCommand = new ReportCommand();
		private static var param : Object;
		private static var titleWindow : TitleWindow;
		private static var parentName : String;
		private var txtWidth : int = 150;
		
		public function ReportCommand() {
			
		}
		
		public static function getInstance(parentName : String) : ReportCommand {
			ReportCommand.parentName = parentName;
			return reportCommand;
		}
		
		public static function getInstanceByTitleWindow(parentName : String , titleWindow : TitleWindow) : ReportCommand {
			ReportCommand.parentName = parentName;
			ReportCommand.titleWindow = titleWindow;
			param.titleWindow = titleWindow;
			return reportCommand;
		}
		
//		public function reqParam(param : Object) : void {
//			titleWindow = param.titleWindow;
//			ReportCommand.param.titleWindow = titleWindow;
//		}
		
		public function queryHeadData(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null && "" != lastResult) {
				
				if(param.headCard == null) {
					param.headCard = createHeadCard(param.parentCanvas);
					param.headCard.visible = false;
				}
				
				var result : Array = JSON.decode(lastResult) as Array;
				var headResult : Array = new Array();
				for each(var obj : Object in result) {
					var headData : Object = new Object();
					headData.period = obj.period;
					headData.billtype = obj.billtype;
					headData.pk_statistic = obj.pk_statistic;
					headResult.push(headData);
					
				}
				
				var dataProvider : ArrayCollection = new ArrayCollection(headResult);
				param.headGrid.dataProvider = dataProvider;
				
				if(result != null && result.length > 0) {
					
					param.headGrid.selectedIndex = 0;
					
					Button(param.headCanvas.getChildByName(parentName + "_btnDelete")).enabled = true;
					
					TextInput(param.headCard.getChildByName(parentName + "_txtPeriod")).text = result[0].period;
					TextInput(param.headCard.getChildByName(parentName + "_txtBillType")).text = result[0].billtype;
					
					var tableServer : RemoteObject = new RemoteObject;
					tableServer.destination = IProcessor.TableProcessor;
					tableServer.queryBodyData(result[0].pk_statistic , parentName);
					tableServer.addEventListener(ResultEvent.RESULT , queryBodyData);
					
				} else {
					PopUpManager.removePopUp(titleWindow);
					PopUpManager.removePopUp(hrreport.waitWindow);
					
					TextInput(param.headCard.getChildByName(parentName + "_txtPeriod")).text = null;
					param.bodyGrid.dataProvider = null;
					
				}
			} else {
				PopUpManager.removePopUp(titleWindow);
				PopUpManager.removePopUp(hrreport.waitWindow);
			}
		}
		
		public function queryBodyData(event : ResultEvent) : void {
			
			var lastResult : String = event.result as String;
			
			if(lastResult != null && "" != lastResult) {
				param = initParam("queryBodyData");
				
				var result : Array = JSON.decode(lastResult) as Array;
				var dataProvider : ArrayCollection = new ArrayCollection(result);
				param.bodyGrid.dataProvider = dataProvider;
				
			} 
			
			PopUpManager.removePopUp(titleWindow);
			PopUpManager.removePopUp(hrreport.waitWindow);
			
		}
		
		public function createHeadCard(parentCanvas : Canvas) : Canvas {
			
			var parentName : String = parentCanvas.name.split("_")[0];
			
			var canvas : Canvas = new Canvas();
			canvas.name = parentName + "_headCard";
			canvas.top = 30;
			canvas.left = 5;
			canvas.right = 5;
			canvas.height = 200;
			canvas.autoLayout = true;
			
			var lblBillno : Label = new Label();
			lblBillno.name = parentName + "_lblBillType";
			lblBillno.width = 60;
			lblBillno.top = 10;
			lblBillno.left = 10;
			lblBillno.text = "报表类型";
			
			var txtBillno : TextInput = new TextInput();
			txtBillno.name = parentName + "_txtBillType";
			txtBillno.visible = true;
			txtBillno.enabled = false;
			txtBillno.width = txtWidth;
			txtBillno.height = 25;
			txtBillno.left = lblBillno.width + 20;
			txtBillno.top = 5;
			
			var lblPeriod : Label = new Label();
			lblPeriod.top = 10;
			lblPeriod.name = parentName + "_lblPeriod";
			lblPeriod.width = 60;
			lblPeriod.left = txtBillno.left + txtBillno.width + 15;
			lblPeriod.text = "统计期间";
			
			var txtPeriod : TextInput = new TextInput();
			txtPeriod.top = 5;
			txtPeriod.name = parentName + "_txtPeriod";
			txtPeriod.enabled = false;
			txtPeriod.visible = true;
			txtPeriod.width = txtWidth;
			txtPeriod.height = 25;
			txtPeriod.left = lblPeriod.left + lblPeriod.width + 10;
			
			canvas.addChild(lblBillno);
			canvas.addChild(txtBillno);
			canvas.addChild(lblPeriod);
			canvas.addChild(txtPeriod);
			
			parentCanvas.addChild(canvas);
			
			return canvas;
		}
		
		public function setTitleWindow(titleWindow : TitleWindow) : void {
			ReportCommand.titleWindow = titleWindow;
		}
		
		public function initParam(initType : String) : Object {
			
			if(param == null)
				param = new Object();
			
			try { param.parentCanvas = Canvas(hrreport.ui.mainNavigator.getChildByName(parentName + "_canvas")); } catch(e : Error) { param.parentCanvas = null; }
			try { param.bodyCanvas = Canvas(param.parentCanvas.getChildByName(parentName + "_bodyCanvas")); } catch(e : Error) { param.bodyCanvas = null; }
			try { param.bodyGrid = DataGrid(param.bodyCanvas.getChildByName(parentName + "_bodyGrid")); } catch(e : Error) { param.bodyGrid = null; }
			try { param.headGrid = DataGrid(param.bodyCanvas.getChildByName(parentName + "_headGrid")); } catch(e : Error) { param.headGrid = null; }
			try { param.headData = param.headGrid.dataProvider; } catch(e : Error) { param.headData = null; }
			try { param.period = param.headGrid.selectedItem.period; } catch(e : Error) { param.period = null; }
			try { param.headCard = Canvas(param.parentCanvas.getChildByName(parentName + "_headCard")); } catch(e : Error) { param.headCard = null; }
			try { param.statisticPeriod = TextInput(param.headCard.getChildByName(parentName + "_txtPeriod")).text; } catch(e : Error) { param.statisticPeriod = null; } 
			try { param.headCanvas = Canvas(param.parentCanvas.getChildByName(parentName + "_headCanvas")); } catch(e : Error) { param.headCanvas = null; }
			
			if(initType == "onBoAdd") {
				try { param.oldHeadData = param.headGrid.dataProvider as ArrayCollection; } catch(e : Error) { param.oldHeadData = null; }
				try { param.oldBodyData = param.bodyGrid.dataProvider as ArrayCollection; } catch(e : Error) { param.oldBodyData = null; }
				try { param.oldTxtBillType = TextInput(param.headCard.getChildByName(parentName + "_txtBillType")).text; } catch(e : Error) { param.oldTxtBillType = null; }
				try { param.oldTxtPeriod = TextInput(param.headCard.getChildByName(parentName + "_txtPeriod")).text; } catch(e : Error) { param.oldTxtPeriod = null; }
			}
			
			try { param.queryCard = Canvas(titleWindow.getElementAt(0)); } catch(e : Error) { param.queryCard = null; }
			try { param.periodByQuery = TextInput(param.queryCard.getChildByName(parentName + "_txtQueryPeriod")).text; } catch(e : Error) { param.periodByQuery; }
			
			return param;
		}
		
	}
}